(function () {
    "use strict"; //JavaScript代码标志为“严格模式
    var suitePageController=function ($scope, $http,$location,suiteService) {
        $scope.suiteList = [];
        suiteService.suiteFindAll().success(function (data) {
            if(data.errorMsg != null){
                $.Huimodalalert(data.errorMsg,1000);
            } else {
                $scope.bchList = data.bchList;
                $scope.otherList = data.otherList;
            }
        });
        $scope.changeUrl = function (item,index) {
            if(item != null && item != undefined && item.id != null){
                window.myiframe.location.href="suite_use.html?id="+item.id;
            } else {
                window.myiframe.location.href="suite_use.html";
            }
            $(".pointer").removeClass("left-hover").eq(index).addClass("left-hover");
        };
    };
    var suiteDetailController=function ($scope, $http,$location,suiteService,toolService) {
        $scope.list = [];
        $scope.vm = {
            id:toolService.parameter("id",window.location.href)
        };
        $scope.getSuite = function () {
            suiteService.suiteGet($scope.vm).success(function (data) {
                if(data.errorMsg != null){
                    $.Huimodalalert(data.errorMsg,1000);
                } else {
                    $scope.suite = data.suite;
                    if(data.suite.watchedBies != null && data.suite.watchedBies.length > 0){
                        $scope.picName = data.suite.watchedBies[0].picName;
                        $scope.picPhone = data.suite.watchedBies[0].picPhone;
                    }
                    var list = data.suite.productsContaineds;
                    angular.forEach(list,function (item) {
                        var versionList = item.productVersion.split("/");
                        var productLatestUpdateTime = item.productLatestUpdateTime;
                        if(productLatestUpdateTime != null){
                            var date = new Date(productLatestUpdateTime);
                            date = date.format("yyyy-MM-dd hh:mm:ss")
                        }
                        angular.forEach(versionList,function (version) {
                            if(version != ""){
                                var bean = {
                                    version:version,
                                    productLatestUpdateTime:date
                                };
                                $scope.list.push(bean);
                            }
                        });
                    });
                }
            });
        };
        $scope.getSuite();
    };
    var suiteUseController=function ($scope, $http,$location,$modal,suiteService,toolsService,downloadService) {
        $scope.loading = false;
        $scope.vm = {
            id:toolsService.parameter("id",window.location.href),
            pageSize:10,
            pageNumber:1
        };
        if($scope.vm.id != null){
            $scope.isDetail = true;
            suiteService.suiteFindYum({id:$scope.vm.id}).success(function (data) {
                if(data.errorMsg != null){
                    $.Huimodalalert(data.errorMsg,1000);
                } else {
                    $scope.yumList = data.result;
                }
            });
        } else {
            $scope.isDetail = false;
            suiteService.suiteFindByBch({suiteName:"bch"}).success(function (data) {
                if(data.errorMsg != null){
                    $.Huimodalalert(data.errorMsg,1000);
                } else {
                    $scope.list = data.result;
                }
            });
        }

        $scope.patch = function (suite) {
            suite.type = "1";
            $scope.loading = true;
            downloadService.fileDownloadDir(suite).success(function (data) {
                $scope.loading = false;
                if(data.errorMsg != null){
                    $.Huimodalalert(data.errorMsg,2000);
                } else {
                    suite.fileName = data.fileName;
                    $scope.download(suite,1);
                }
            });
        };

        $scope.download = function(suite,type){
            if (type == 1){
                window.location.href = "file/download?type=1&fileName=" + suite.fileName;
            } else if(type == 2){
                window.location.href = "file/download?type=2&fileName=" + suite.version;
            } else {
                var path = suite.parent+"/"+suite.fileName;
                //特殊字符+，&转义处理
                path = path.replace(/\+/g, "%2B");
                path = path.replace(/\&/g, "%26");
                window.location.href = "file/download?type=3&fileName=" + path;
            }
        };
    };
    angular.module("cmss.suite",[])
        .controller("SuitePageController",["$scope","$http","$location","SuiteService",suitePageController])
        .controller("SuiteUseController",["$scope","$http","$location","$modal","SuiteService","toolsService","DownloadService",suiteUseController])
        .controller("SuiteDetailController",["$scope","$http","$location","SuiteService","toolsService",suiteDetailController])
        .service("toolsService",[function () {
            "use strict";
            /**
             * 获取网址上的参数
             * @param name  网址上的参数.例如（id）
             * @param url   网址连接。
             * @returns {*}  返回网址参数的值
             */
            this.parameter = function (name, url) {
                var parameterString = null;
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
                parameterString = url.substr(url.indexOf("?"), url.length);//获取截取？以后的字符串
                var r = parameterString.substr(1).match(reg);
                if (r != null){
                    return  unescape(r[2]);
                }
                return null;
            };

            //日期转换函数
            Date.prototype.format = function(format){
                var o = {
                    "M+" : this.getMonth()+1, //month
                    "d+" : this.getDate(),    //day
                    "h+" : this.getHours(),   //hour
                    "m+" : this.getMinutes(), //minute
                    "s+" : this.getSeconds(), //second
                    "q+" : Math.floor((this.getMonth()+3)/3),  //quarter
                    "S" : this.getMilliseconds() //millisecond
                };
                if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
                    (this.getFullYear()+"").substr(4 - RegExp.$1.length));
                for(var k in o)if(new RegExp("("+ k +")").test(format))
                    format = format.replace(RegExp.$1,
                        RegExp.$1.length==1 ? o[k] :
                            ("00"+ o[k]).substr((""+ o[k]).length));
                return format;
            };

            Array.prototype.insert = function(index, value) {
                if(index < 0){
                    //如果index位置无效,默认插入到最后一个位置上
                    index = this.length;
                }
                var part1 = this.slice(0, index);
                var part2 = this.slice(index);
                part1.push(value);
                return (part1.concat(part2));
            };
            Array.prototype.remove = function(index) {
                //检查index位置是否有效
                if(index >= 0 && index < this.length){
                    var part1 = this.slice(0, index);
                    var part2 = this.slice(index);
                    part1.pop();
                    return (part1.concat(part2));
                }
                return this;
            };
        }])
})();
